package vn.anhqn.englishsentensetranslator.base.activity;

import vn.anhqn.englishsentensetranslator.R;
import vn.anhqn.englishsentensetranslator.constant.Constant;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager.BackStackEntry;
import android.util.Log;

public class BaseFragmentActivity extends FragmentActivity {
	protected boolean isForeground = true;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_main);
	}

	public void addFirstFragment(Class<? extends Fragment> fragment) {
		Fragment fm = null;
		try {
			fm = fragment.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		if (fm != null) {
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction
					.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out, R.anim.slide_left_in, R.anim.slide_right_out);
			transaction.add(R.id.container, fm);
			transaction.commit();
		}
	}

	public void replaceNewInstance(Class<? extends Fragment> fragment) {
		FragmentManager fragmentManager = this.getSupportFragmentManager();
		// if framgnet exists from backstack pop it
		boolean isPopOK = fragmentManager.popBackStackImmediate(fragment.getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
		Log.v("popimmediate", "pop: " + isPopOK);
		switchFragment(fragment);

	}

	public void replaceNewInstance(Class<? extends Fragment> fragment, Bundle bundle) {
		FragmentManager fragmentManager = this.getSupportFragmentManager();
		// if framgnet exists from backstack pop it
		fragmentManager.popBackStackImmediate(fragment.getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
		switchFragment(fragment, bundle);

	}

	public int switchFragment(Class<? extends Fragment> fragment, String tag, Bundle pBundle, boolean isAddBackTack) {
		FragmentManager fragmentManager = this.getSupportFragmentManager();
		boolean isPopOk = fragmentManager.popBackStackImmediate(tag, 0);

		if (!isPopOk) {
			Fragment fragmentInstance = null;
			try {
				fragmentInstance = fragment.newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			if (fragmentInstance == null) {
				Log.v(tag, "can't new instance of fragment");
				return Constant.UNKNOW_INSTANCE_FRAGMENT;
			}
			if (pBundle != null) {
				fragmentInstance.setArguments(pBundle);
			}

			FragmentTransaction transaction = fragmentManager.beginTransaction();
			transaction
					.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out, R.anim.slide_left_in, R.anim.slide_right_out);
			transaction.replace(R.id.container, fragmentInstance);
			if (isAddBackTack) {
				transaction.addToBackStack(tag);
			}
			return transaction.commit();
		}
		return Constant.EXIT_FRAGMENT_IN_BACKSTACK;
	}

	public int switchFragment(Class<? extends Fragment> fragment, Bundle pBundle, boolean isAddBackTack) {
		return this.switchFragment(fragment, fragment.getName(), pBundle, isAddBackTack);
	}

	public int switchFragment(Class<? extends Fragment> fragment, Bundle pBundle) {
		return this.switchFragment(fragment, fragment.getName(), pBundle, true);
	}

	public int switchFragment(Class<? extends Fragment> fragment) {
		return this.switchFragment(fragment, fragment.getName(), null, true);
	}

	protected boolean isFragmentCurrentExit(String tag) {
		boolean ret = false;
		int countFragmentInBackStack = this.getSupportFragmentManager().getBackStackEntryCount();
		if (countFragmentInBackStack > 0) {
			BackStackEntry currentEntry = this.getSupportFragmentManager().getBackStackEntryAt(countFragmentInBackStack - 1);
			if (currentEntry != null && currentEntry.getName().equals(tag)) {
				ret = true;
			}
		}
		return ret;
	}

	public void finishCurrentFragment() {
		getSupportFragmentManager().popBackStack();
	}

	public boolean existsFragment() {
		if (getSupportFragmentManager().findFragmentById(R.id.container) != null) {
			return true;
		}
		return false;
	}
}
